const {ESLINT_MODES, whenDev, whenProd } = require("@craco/craco");
const CracoLessPlugin = require('craco-less');
const TerserPlugin=require("terser-webpack-plugin");
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const { getThemeVariables } = require('antd/dist/theme');
const path = require('path')
// 判断编译环境是否为生产
const pathResolve = pathUrl => path.join(__dirname, pathUrl)
module.exports = {
    eslint: {
        mode: ESLINT_MODES.file
    },
    babel: {
        plugins: [
            ['@babel/plugin-proposal-decorators', { legacy: true }],
            ['import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css' }]
        ]
    },
    webpack:{
        "react-dom": "@hot-loader/react-dom",
        plugins:[
            ...whenDev(() => [], []),
            ...whenProd(() => [
                new TerserPlugin({
                    sourceMap: true, // Must be set to true if using source-maps in production
                    terserOptions: {
                        ecma: undefined,
                        warnings: false,
                        parse: {},
                        compress: {
                            drop_console:true, // 生产环境下移除控制台所有的内容
                            drop_debugger: true, // 移除断点
                            pure_funcs:["console.log", "console.warn", "console.error"], // 生产环境下移除console
                        }
                    },
                    extractComments:false, //删除注释
                }),
                // 打压缩包
                new CompressionWebpackPlugin({
                    filename: '[path].gz[query]',
                    algorithm: 'gzip',
                    test: /\.(js|css)(\?.*)?$/i,
                    threshold: 10240, // 对超过10k的数据进行压缩
                    minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
                    deleteOriginalAssets: false, // 删除原文件
                })
            ], [])
        ]
    },
    config: {
        "commitizen": {
            "path": "./node_modules/cz-conventional-emoji"
        }
    },
    configure: (webpackConfig, { env, paths }) => {
        webpackConfig.devtool = 'source-map';
        return webpackConfig;
    },
    plugins: [
        {
            plugin: CracoLessPlugin,
            options: {
                lessLoaderOptions: {
                    lessOptions: {
                        modifyVars: getThemeVariables({
                            dark: true,
                            compact: false
                        }),
                        javascriptEnabled: true,
                    },
                }
            },
        }
    ],
    externals:{
        CityGis:"CityGis"
    },
    devServer: {
        proxy: {
            "/apiColl": {
                target: "https://www.maicedata.com/collector",
                changeOrigin: true,
                pathRewrite: {
                    "^/apiColl": ""
                }
            },
        }
    }
};
